<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use ImiApp\ApiServer\Api\Model\User;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
// use ImiApp\ApiServer\Common\AuthApi;
use Imi\Db\Db;


/**
 * @OA\Info(title="测试接口", version="0.1")
 * @Auth(name="token",nologin={"uuid","get_user_token","test","token_user_id","get_user_qr"})
 * @Controller("/test/")
 */
class Test extends Tpmecms
{




    /**
     * 获取运费模版的运费
     * @Action
     * @param int $dispatch_ids 模版ids
     * @param int $addr_id 地址
     * @return void
     */
    public function dispatchAmount($dispatch_ids=null, $addr_id = null)
    {
//        for ($i = 5;$i<23;$i++){
//            for ($d = 1;$d<25;$d++){
//                $运费 = $this->GetDispatchAmount((string)$i, $d);
//                if($运费>0){
//
//                    var_dump('模版id'.$i.'地址id'.$d.'运费'.$运费);
////                    return $this->success('运费', [
////                        'price' => $运费,
////                        $i => $dispatch_ids,
////                        $d => $addr_id,
////                    ]);
//                }
//            }
//        }
        $运费 = $this->GetDispatchAmount($dispatch_ids, $addr_id);
        return $this->success('运费', [
            'price' => $运费,
            'dispatch_ids' => $dispatch_ids,
            'addr_id' => $addr_id,
        ]);
    }


    /**
     * 测试
     * @Action
     * @Route(url="uuid",method="GET")
     * @return void
     */
    public function uuid()
    {
        return $this->success('uuid', [

            'msg' => \ImiApp\ApiServer\Common\Random::uuid(),
            'date' => date('Y-m-d H:i:s'),
        ]);
    }

    /**
     * 找用户
     * @Action
     * @Route(url="user_find",method="GET")
     * @param int $id 用户id
     * @return void
     */
    public function user_find($id)
    {
        $user = new \ImiApp\ApiServer\Api\Model\FaUser;
        return $this->success('user_find', [

            'id' => $id,
            'user' => $user::find($id),
            'date' => date('Y-m-d H:i:s'),
        ]);
    }

    /**
     * 更新用户邀请码
     * @Action
     * @Route(url="get_user_qr",method="GET")
     * @param int $id 用户id
     * @return void
     */
    public function get_user_qr($id)
    {
        var_dump('id', $id);
        $this->UpdateUserInviteCode($id);
    }



    /**
     * 获取用户token
     * @Action
     * @Route(url="get_user_token",method="GET")
     * @return void
     */
    public function get_user_token()
    {
        $id = 111;
        $Token = new \ImiApp\ApiServer\Common\Token;
        $token = \ImiApp\ApiServer\Common\Random::uuid(); //\ImiApp\ApiServer\Common\Random
        $Token::set($token, $id, 999999);
        return $this->success($id, [

            'token' => $token

        ]);
    }

    /**
     * 删除用户token
     * @Action
     * @Route(url="delete_user_token",method="GET")
     * @return void
     */
    public function delete_user_token()
    {
        $token = "2482a2ab-ac0b-48dc-a8c2-53a2a8631f8c";
        $Token = new \ImiApp\ApiServer\Common\Token;
        $data = $Token::delete($token);
        return $this->success($token, [
            'data' => $data
        ]);
    }

    /**
     * token获取用户id
     * @Action
     * @Route(url="token_user_id",method="GET")
     * @return void
     */
    public function token_user_id()
    {
        $token = "2482a2ab-ac0b-48dc-a8c2-53a2a8631f8c";
        $Token = new \ImiApp\ApiServer\Common\Token;
        // $token = \ImiApp\ApiServer\Common\Random::uuid(); //\ImiApp\ApiServer\Common\Random
        $data = $Token::get($token);
        return $this->success($token, [
            'data' => $data
        ]);
    }


    /**
     * 清空数据
     * @Action
     * @return void
     */
    public function del_db()
    {

        $table_arr = [
            // "activity_groupon_log",
            // "admin_log",
            // "goods_stock_log",
            // "prize_log",
            // "refund_log",
            // "request_log",
            "run_excite_money_log",
            "run_money_log",
            "run_score_log",
            "run_shell_log",
            // "share_score_log",
            // "store_log",
            "store_money_log",
            "store_score_log",
            "store_shell_log",
            "store_surety_log",
            "sysinfo_money_log",
            "sysinfo_score_log",
            "sysinfo_shell_log",
            "user_money_log",
            // "user_pid_log",
            "user_score_log",
            // "user_set_temp_log",
            "user_shell_log",
            "user_surety_log",
            // "user_upgrade_set_log",
            // "so_admin_login_log",
            // "so_admin_operate_log"
        ];
        $ci = 0;
        foreach ($table_arr as $key => $table) {
            $res = Db::name($table)->where('id', '>', 0)->delete();

            if ($res) {
                $ci++;
                $arr[] = $table;
            }
        }
        return $this->success($ci, $arr);
    }


    /**
     * 查询sql
     * @param string $sql 
     * @Action
     * @return void
     */
    public function sql_db($sql=null)
    {
        var_dump($sql);
        // if(!$sql)
        // $sql = "SHOW VARIABLES LIKE 'max_wait_timeout'";
        // $res = Db::exec($sql);
        // var_dump($res);
    }


    /**
     * @Action
     * @Route(url="doc",method="GET")
     * @return void
     */
    public function doc()
    {
        // return $this->success('doc',__DIR__);
        // $openapi = \OpenApi\Generator::scan("/www/wwwroot/pddadmin.he4966.cn/imi-admin/ApiServer/Api/Controller");
        $html = <<<LABEL
        <!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css">
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32"/>
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16"/>
    <style>
        html {
            box-sizing: border-box;
            overflow: -moz-scrollbars-vertical;
            overflow-y: scroll;
        }

        *,
        *:before,
        *:after {
            box-sizing: inherit;
        }

        body {
            margin: 0;
            background: #fafafa;
        }
    </style>
</head>

<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js" charset="UTF-8"></script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"></script>
<script>
    window.onload = function () {
        console.log(SwaggerUIBundle.plugins)
        // Begin Swagger UI call region
        const ui = SwaggerUIBundle({
            // url: "http://" + location.host + "/imi-admin/api.json",
            url: "http://pddadmin.he4966.cn/imi-admin/api.json",
            dom_id: '#swagger-ui',
            dom_id: '#swagger-ui',
            deepLinking: true,
            docExpansion: 'list',
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],
            layout: "StandaloneLayout",
            filter: true,
            defaultModelsExpandDepth: 0
        })
        // End Swagger UI call region

        window.ui = ui
    }
</script>
</body>
</html>

LABEL;
        header('Content-Type: text/html;charset=UTF-8');

        return $html;
        // return $this->success('doc',$openapi->toYaml());
    }

    /**
     * 文件日志
     *
     * @return void
     */
    public static function file_log()
    {
        file_put_contents('file_log.txt', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
    }
}
